package 二分查找;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode658_找到K个最接近的元素 {

    public List<Integer> findClosestElements(int[] arr, int k, int x) {
        List<Integer> list = new ArrayList<Integer>();
        if(k > arr.length) {
            return list;
        }
        int left = 0;
        int right = arr.length-1;
        while(right-left+1 > k) {
            if(x - arr[left] <= arr[right] - x) {
                right--;
            } else {
                left++;
            }
        }
        for(int i = left; i <= right; i++) {
            list.add(arr[i]);
        }


        return list;
    }

}
